package com.hongkzh.www.look.media.view.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.hongkzh.www.R;
import com.hongkzh.www.look.media.model.bean.CarouselBean;
import com.hongkzh.www.look.media.model.bean.Top10Bean;
import com.hongkzh.www.look.media.model.bean.UserCategoryBean;
import com.hongkzh.www.look.media.presenter.MediaPresenter;
import com.hongkzh.www.look.media.view.adapter.MedRvAdapter;
import com.hongkzh.www.look.media.view.iview.MediaIView;
import com.hongkzh.www.other.recyclerview.OnVerticalScrollListener;
import com.hongkzh.www.other.utils.SharedPreferencesUtil;
import com.hongkzh.www.other.utils.UIUtils;
import com.hongkzh.www.view.activity.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 自媒体广告
 */
public class MediaActivity extends BaseActivity<MediaPresenter> implements MediaIView, View.OnClickListener {

    @BindView(R.id.med_rv)
    RecyclerView medRv;
    @BindView(R.id.med_title)
    TextView medTitle;
    @BindView(R.id.med_ll)
    LinearLayout medLl;
    @BindView(R.id.med_back)
    ImageView medBack;
    @BindView(R.id.med_pop)
    RadioButton medPop;
    @BindView(R.id.med_near)
    RadioButton medNear;
    @BindView(R.id.med_cir)
    RadioButton medCir;
    @BindView(R.id.med_rg)
    RadioGroup medRg;
    private String loginUid;
    private MedRvAdapter medRvAdapter;
    private OnVerticalScrollListener onVerticalScrollListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_media;
    }

    @Override
    protected void init() {
        loginUid = new SharedPreferencesUtil(UIUtils.getContext()).getUserInfo().getLoginUid();

        setPresenter(new MediaPresenter());
        getPresenter().getCarouselList()
                .getUserCategoryList(loginUid)
                .getTop10List();

        medLl.getBackground().mutate().setAlpha(0);
        medTitle.setText(R.string.title_media);
        medRvAdapter = new MedRvAdapter(this, medRg, getSupportFragmentManager());
        medRv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        medRv.setAdapter(medRvAdapter);
    }

    @Override
    protected void setListener() {
        onVerticalScrollListener = new OnVerticalScrollListener() {
            @Override
            public void onScrolledDown() {
                super.onScrolledDown();
                medLl.getBackground().mutate().setAlpha(255);
            }

            @Override
            public void onScrolledToTop() {
                super.onScrolledToTop();
                medLl.getBackground().mutate().setAlpha(0);
            }

            @Override
            public void onScrolledVisible() {
                super.onScrolledVisible();
                medRg.setVisibility(View.VISIBLE);
            }

            @Override
            public void onScrolledGone() {
                super.onScrolledGone();
                medRg.setVisibility(View.GONE);
            }
        };
        onVerticalScrollListener.setHeight(medLl.getMeasuredHeight());
        medBack.setOnClickListener(this);
        medRv.setOnScrollListener(onVerticalScrollListener);
    }

    @Override
    public void setCarouselList(CarouselBean carouselBean) {
        medRvAdapter.addCarouselList(carouselBean);
        medRvAdapter.notifyItemChanged(0);
    }

    @Override
    public void setUserCategoryList(UserCategoryBean userCategoryBean) {
        medRvAdapter.addUserCategoryList(userCategoryBean);
        medRvAdapter.notifyItemChanged(1);
    }

    @Override
    public void setTop10List(Top10Bean top10Bean) {
        medRvAdapter.addTop10List(top10Bean);
        medRvAdapter.notifyItemChanged(2);
    }

    @Override
    public void onError(Exception e) {

    }

    @Override
    public void onClick(View v) {
        this.finish();
    }

    @Override
    protected void onPause() {
        medRv.removeOnScrollListener(onVerticalScrollListener);
        super.onPause();
    }
}
